/*
 * AboutDialog.java
 *
 *  created: 7.10.2011
 *  charset: UTF-8
 *  license: MIT (X11) (See LICENSE file for full license)
 */
package cz.mp.k3bg.gui;

import cz.mp.k3bg.log.LoggerManager;
import java.util.logging.Logger;
import javax.swing.JTextPane;
import cz.mp.k3bg.Application;
import cz.mp.k3bg.Constants;
import cz.mp.k3bg.Credits;
import cz.mp.k3bg.misc.HyperlinkHandler;
import cz.mp.util.GuiUtils;
import cz.mp.util.ResourceUtils;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.io.IOException;
import java.net.URL;
import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.text.html.HTMLEditorKit;
import static cz.mp.k3bg.Application.*;
import static cz.mp.k3bg.TextSource.*;


/**
 * Dialog "O programu".
 * 
 * @author Martin Pokorný
 * @version 0.1
 */
public class AboutDialog extends JDialog {

    private static final boolean DEBUG = false;
    private static final Logger logger =
            LoggerManager.getLogger(AboutDialog.class, DEBUG);

    private static AboutDialog instance = null;  
    
    private JTabbedPane tabbedPane = new JTabbedPane(
            JTabbedPane.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
    
    private JPanel tabDetailPanel = new JPanel();
    private JPanel tabLicencePanel = new JPanel();
    private JPanel tabCreditsPanel = new JPanel();
    
    // -----
    
    /** */
    private AboutDialog() {
        super(MainFrame.getInstance(), ModalityType.APPLICATION_MODAL);
        initComponents();
        initLayout();
        initDialog();
    }

    /**
     * 
     * @return
     */
    private static AboutDialog getInstance() {
        if (instance == null) {
            instance = new AboutDialog(); 
        }
        return instance;
    }

    /**
     *
     */
    public static void showDialog()  {
        getInstance();
        instance.setVisible(true);
    }


    /**
     *
     */
    private void initDialog() {
        setTitle(getLocText("gui.about.title") + " " + Application.NAME);

        setMinimumSize(new Dimension(320, 180));
        setSize(430, 270);

        setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);

        setLocationRelativeTo(MainFrame.getInstance());
        GuiUtils.addHideActionWithEscapeKey(this);
    }

    /**
     * 
     */
    private void initComponents() {
        fillTabDetailPanel();
        fillTabLicencePanel();
        fillTabCreditsPanel();
                
        tabbedPane.addTab(getLocText("gui.about.main"), tabDetailPanel);
        tabbedPane.addTab(getLocText("gui.about.licence"), tabLicencePanel);
        tabbedPane.addTab(getLocText("gui.about.credits"), tabCreditsPanel);
    }    

    /**
     *
     */
    private void initLayout() {
        this.setLayout (new GridBagLayout());

        this.add(tabbedPane, new GridBagConstraints(0,0,5,1,1.0,1.0,
                GridBagConstraints.WEST, GridBagConstraints.BOTH,
                new Insets(5, 0, 0, 0), 0,0));
    }

    /**
     *
     */
    private void fillTabDetailPanel() {
        String text = getLocText("gui.about.main.text",
                Constants.KINDLEGEN_DOWNLOAD_URL,
                HOMEPAGE_URL,
                VERSION_FULL_NAME,
                LICENCE_NAME,
                JRE_VERSION + " (" + JVM_VENDOR + ")");
        fillTabPane(tabDetailPanel, text);
    }

    /**
     * 
     */
    private void fillTabCreditsPanel() {
        fillTabPane(tabCreditsPanel, Credits.getCreditsText());
    }

    /**
     *
     */
    private void fillTabLicencePanel() {
        String resourcePath = "/resources/files/license.html";
        fillTabPaneFromResources(tabLicencePanel, resourcePath);
    }

    /**
     *
     * @param panel
     * @param content
     */
    private void fillTabPaneFromResources(JPanel panel, String resourcePath) {
        JTextPane textPane = new JTextPane();
        JScrollPane scrollPane = new JScrollPane(textPane);
        adjustEditorPane(textPane, scrollPane);

        try {
            URL licenceUrl = ResourceUtils.getUrl(resourcePath);
            textPane.setPage(licenceUrl);
        } catch (IOException ex) {
            logger.warning(ex.toString());
            textPane.setText("<HTML><B>Error: " + resourcePath + " can't be readed!");
        }
        GuiUtils.setDefaultFont(textPane);

        panel.setLayout(new BorderLayout());
        panel.add(scrollPane, BorderLayout.CENTER);
    }

    /**
     *
     * @param panel
     * @param content
     */
    private void fillTabPane(JPanel panel, String content) {
        JTextPane textPane = new JTextPane();
        JScrollPane scrollPane = new JScrollPane(textPane);
        adjustEditorPane(textPane, scrollPane);
       
        textPane.setText(content);
        GuiUtils.setDefaultFont(textPane);

        panel.setLayout(new BorderLayout());
        panel.add(scrollPane, BorderLayout.CENTER);
    }

    /**
     * 
     * @param editorPane
     * @param editorPaneScroll 
     */
    private void adjustEditorPane(JEditorPane editorPane, JScrollPane editorPaneScroll) {
        editorPane.setContentType(new HTMLEditorKit().getContentType());

//        GuiUtils.setDefaultFont(editorPane);

        editorPane.addHyperlinkListener(new HyperlinkHandler());
        editorPane.setEditable(false);

        editorPane.setOpaque(true);
//        editorPane.setOpaque(false);
        editorPane.setBackground(UIManager.getColor("Panel.background"));
        
        editorPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

//        editorPaneScroll.setViewportView(editorPane);

        editorPaneScroll.setBorder(null);
        editorPaneScroll.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        editorPaneScroll.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
    }

}   // AboutDialog.java

